package com.mycompany.app.common.ajax.autocompleter.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.transaction.annotation.Transactional;

import com.mycompany.app.common.ajax.autocompleter.dao.ICountryDao;
import com.mycompany.app.common.ajax.autocompleter.domain.Country;
import com.mycompany.app.dao.hibernate.GenericDaoHibernate;

@Transactional
public class CountryDaoHibernate extends GenericDaoHibernate<Country, Long>
        implements ICountryDao {

    public CountryDaoHibernate() {

        super(Country.class);
        // TODO Auto-generated constructor stub
    }
    
    public CountryDaoHibernate(Class<Country> persistentClass) {

        super(persistentClass);
        // TODO Auto-generated constructor stub
    }

    @SuppressWarnings("unchecked")
    /**
     * query Country name which start with input value
     */
    public List<Country> queryCountry(final String value) {
        List list = getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {

                Query query = session
                        .createQuery("from Country c where c.value like ? ");
//                query.setFirstResult(0);
                query.setMaxResults(10);
                query.setString(0, value+"%");
                List list = query.list();
                return list;
            }
        });
        return list;
    }

}
